TWB.New_Module({
	///////////////////////////
	name : "Session",        //
	author : "enduo",        //
	version : 1.0,           //
	///////////////////////////
	
	priority : 10,
	Save : function(){
		if(TWB.logged(window._content)){
			// PATCH VILLAGE LIST TO SESSION
			var V=TWB.gei("TWB-VillageList").childNodes;
			var i;
			TWB._SESSION.VILLAGES=[];
			for(i=0; i<V.length; i++){
				TWB._SESSION.VILLAGES.push({
					id: V[i].getAttribute("id").split("TWB-v")[1],
					label: V[i].getAttribute("label"),
					tooltip: V[i].getAttribute("tooltiptext")
				});
			}
			TWB.set_var("session_"+TWB.language+"_"+TWB.World(),TWB._SESSION);
			alert(TWB.lang("sessionsaved"));
		}
		else{
			alert(TWB.lang("notlogged"));
		}
	},
	Load : function(silent){
		if(TWB.logged(window._content)){
			if(typeof silent=="undefined" && !confirm(TWB.lang("confirmload",[TWB.language+TWB.World()]))){
				return;
			}
			TWB._SESSION=TWB.get_var("session_"+TWB.language+"_"+TWB.World(),{});
			// Load Village List now
			TWB.Modules.Villages.ClearAll();
			var i;
			
			if(typeof TWB._SESSION.VILLAGES=="undefined"){
				return;
			}
			
			for(i=0; i<TWB._SESSION.VILLAGES.length; i++){
				TWB.Modules.Villages.NewVil({
					id : "TWB-v"+TWB._SESSION.VILLAGES[i].id,
					label : TWB._SESSION.VILLAGES[i].label,
					tooltiptext : TWB._SESSION.VILLAGES[i].tooltip,
					value : TWB._SESSION.VILLAGES[i].label+" "+TWB._SESSION.VILLAGES[i].tooltip
				});
			}
			// Update current village
			TWB.Modules.Villages.UpdateCurVil();
		}
		else{
			if(typeof silent=="undefined"){
				alert(TWB.lang("notlogged"));
			}
		}
	},
	Import : function(){
		if(!TWB.logged()){
			return alert(TWB.lang("notlogged"));
		}
		// Reload
		var nsIFilePicker=TWB.Ci.nsIFilePicker;
		var fp=TWB.Cc["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
		fp.appendFilter(TWB.lang("tffile"),"*.twb.session");
		fp.init(window,TWB.lang("importchoosefile"),nsIFilePicker.modeOpen);
		if(!fp.show()){
			var path=fp.file.path;
			var fo=TWB.Cc["@mozilla.org/file/local;1"].createInstance(TWB.Ci.nsILocalFile);
			fo.initWithPath(path);
			var ist = TWB.Cc["@mozilla.org/network/file-input-stream;1"].createInstance(TWB.Ci.nsIFileInputStream);
			ist.init(fo,0x01,00004,null);
			var szt = TWB.Cc["@mozilla.org/scriptableinputstream;1"].createInstance(TWB.Ci.nsIScriptableInputStream);
			szt.init(ist);
			var data = szt.read(szt.available());
			szt.close();
			TWB.getPrefs().setCharPref("session_"+TWB.language+"_"+TWB.World(), data);
			TWB.Modules.Session.Load(true);
		}
	},
	Export : function(){
		if(!TWB.logged()){
			return alert(TWB.lang("notlogged"));
		}
		var nsIFilePicker=TWB.Ci.nsIFilePicker;
		var fp=TWB.Cc["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
		fp.appendFilter(TWB.lang("tsfile"),"*.twb.session");
		fp.init(window,TWB.lang("exportchoosefile"),nsIFilePicker.modeSave);
		if(!fp.show()){
			var i;
			var vid;
			var data=TWB.getPrefs().getCharPref("session_"+TWB.language+"_"+TWB.World());
			var path=fp.file.path+".twb.session";
			var fo=TWB.Cc["@mozilla.org/file/local;1"].createInstance(TWB.Ci.nsILocalFile);
			fo.initWithPath(path);
			if(fo.exists()){
				// Overwrite
				fo.remove(true);
			}
			fo.create(0,0x0600);
			var ostream = TWB.Cc["@mozilla.org/network/file-output-stream;1"].createInstance(TWB.Ci.nsIFileOutputStream);
			ostream.init(fo, 0x02 | 0x08 | 0x20, 0666, ostream.DEFER_OPEN); 
			var converter = TWB.Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(TWB.Ci.nsIScriptableUnicodeConverter);
			converter.charset = "UTF-8";
			var istream = converter.convertToInputStream(data);
			Components.utils.import("resource://gre/modules/NetUtil.jsm");
			NetUtil.asyncCopy(istream, ostream);
		}
	},
	init : function(){
		TWB._SESSION={};
	},
});
